package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.dto.SupervisorVo;
import com.prd.mapper.EntGoalCollectMapper;
import com.prd.model.EntGoal;
import com.prd.model.EntGoalCollect;
import com.prd.model.EntGoalPeriodic;
import com.prd.model.LoginUser;
import com.prd.service.EntGoalCollectService;
import com.prd.util.UserInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EntGoalCollectServiceImpl extends ServiceImpl<EntGoalCollectMapper, EntGoalCollect> implements EntGoalCollectService {

    @Autowired
    private EntGoalCollectMapper entGoalCollectMapper;

    @Override
    public int insertInfo(EntGoalCollect entGoalCollect) {
        LambdaQueryWrapper<EntGoalCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntGoalCollect::getGoalId, entGoalCollect.getGoalId());
        queryWrapper.eq(entGoalCollect.getStageId() != null, EntGoalCollect::getStageId, entGoalCollect.getStageId());
        EntGoalCollect goalCollect = entGoalCollectMapper.selectOne(queryWrapper);
        if (goalCollect != null) {
            throw new RuntimeException("该记录已经存在！");
        }
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entGoalCollect);
        return entGoalCollectMapper.insert(entGoalCollect);
    }

    @Override
    public int updateInfo(EntGoalCollect entGoalCollect) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entGoalCollect);
        return entGoalCollectMapper.updateById(entGoalCollect);
    }

    @Override
    public int addSupervisor(SupervisorVo supervisorVo) {
        EntGoalCollect entGoalCollect = entGoalCollectMapper.selectById(supervisorVo.getId());
        if (entGoalCollect == null) {
            throw new RuntimeException("记录没有找到！");
        }
        entGoalCollect.setSupervisorReach(supervisorVo.getReach());
        entGoalCollect.setSupervisorGist(supervisorVo.getGist());
        entGoalCollect.setSupervisorContent(supervisorVo.getContent());
        entGoalCollect.setSupervisorRecord(supervisorVo.getRecord());
        entGoalCollect.setSupervisorPicture(supervisorVo.getPicture());
        return updateInfo(entGoalCollect);
    }
}
